//
//  HelpMeDoViewController.m
//  MRBang
//
//  Created by 铅笔 on 15/12/14.
//  Copyright © 2015年 杭州易网新科技. All rights reserved.
//

#import "HelpMeDoViewController.h"
#import "ChooseCreditableCardVC.h"
//#import "HomeViewController.h"//首页

#import "HelpMeDoCell.h"
#import "HelpMeDoModel.h"

@interface HelpMeDoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *mutableArray_data;//存放所有数据

@end

@implementation HelpMeDoViewController

- (NSMutableArray *)mutableArray_data
{
    if (!_mutableArray_data) {
        _mutableArray_data = [[NSMutableArray alloc] init];
    }
    return _mutableArray_data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"帮我办";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    //创建子视图
    [self initWithAllSubviews];
    
    //网络数据请求
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self DataSourceRequestListView];

    //    手势问题
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    
    UISwipeGestureRecognizer *toleftswipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe)];
    toleftswipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:toleftswipe];
    
    //right button subviews
    [self initWithRightButtonSubviews];
    
    [self setUprefresh];
}

#pragma mark --- right button
- (void) initWithRightButtonSubviews
{
    UIButton *button_rightChange = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_rightChange setTitle:@"返回首页"  forState:UIControlStateNormal];
    [button_rightChange setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button_rightChange.frame = CGRectMake(15, 0, 60, 30);
    [button_rightChange.titleLabel setFont:[UIFont systemFontOfSize:24*PROPORTION_WIDTH]];
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button_rightChange];
    self.navigationItem.rightBarButtonItem = menuButton;
    
    [button_rightChange addTarget:self action:@selector(backToHomeViewAction)
                 forControlEvents:UIControlEventTouchUpInside];

}

//返回到首页
- (void) backToHomeViewAction
{
    [self.tabBarController setSelectedIndex:0];
    [self.navigationController popToRootViewControllerAnimated:YES];
//    for (UIViewController *temp in self.navigationController.viewControllers) {
//        
//        if ([temp isKindOfClass:[HomeViewController class]]) {
//            [self.navigationController popToViewController:temp animated:YES];
//        }
//    }

}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 集成下拉刷洗 上拉加载
- (void)setUprefresh
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    //    [self.tableView.header beginRefreshing];
}

#pragma mark - 数据处理相关
#pragma mark 下拉刷新数据
- (void)loadNewData
{
    // 1.添加假数据
    [self.mutableArray_data removeAllObjects];
    [self DataSourceRequestListView];
    
    [self.tableView.header endRefreshing];
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark --- UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mutableArray_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cellHelpMeDo";
    HelpMeDoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[HelpMeDoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.mutableArray_data.count!=0) {
       cell.model_helpMe = self.mutableArray_data[indexPath.row];

    }
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mutableArray_data.count!=0) {
        HelpMeDoModel *model_temp = self.mutableArray_data[indexPath.row];
        
        //将银行卡 id 保存
        [[NSUserDefaults standardUserDefaults] setObject:model_temp.creditcardbankid forKey:@"creditcardbankid"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        ChooseCreditableCardVC *chooseCardVC = [[ChooseCreditableCardVC alloc] init];
        chooseCardVC.str_creditcardbankid = model_temp.creditcardbankid;
        chooseCardVC.str_bankName = model_temp.name;
        
        [self.navigationController pushViewController:chooseCardVC animated:NO];
        //将所有的数据都传递过去
        chooseCardVC.mutableArray_pathDatas = self.mutableArray_data;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 281*PROPORTION_WIDTH;
}

#pragma mark --- 添加所有滑动的手势
- (void)leftSwipe {
    if (self.tabBarController.selectedIndex == 0) {
        //这边根据需求写
        return;
    }
    self.tabBarController.selectedIndex--;
}

- (void)rightSwipe {
    if (self.tabBarController.selectedIndex == self.tabBarController.childViewControllers.count-1) {
        //这边根据需求写
        return;
    }
    self.tabBarController.selectedIndex++;
}

#pragma mark -- 网络数据请求
- (void) DataSourceRequestListView
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        
        NSString *strUrl = [NSString stringWithFormat:@"%@creditcard/bank/get",MRAPI];
        
        [[HttpRequest shardWebUtil] getNetworkRequestURLString:strUrl parameters:nil success:^(id obj) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSLog(@"bankcard%@",obj);
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0) {
                NSArray *array_data = obj[@"data"];
                if (array_data.count!=0) {
                    for (int i=0; i<array_data.count; i++) {
                        HelpMeDoModel *model = [[HelpMeDoModel alloc] init];
                        [model decodeDic:array_data[i]];
                        [self.mutableArray_data addObject:model];
                    }
                }
                else
                {
                    [BaseViewController showMessage:@"暂无数据"];
                }
                
                [self.tableView reloadData];
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            //        [BaseViewController showMessage:error];
            [BaseViewController showMessage:@"请求超时"];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }];

    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
